<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    function ArrayList() {
      this.array = []

      ArrayList.prototype.insert = function (item) {
        this.array.push(item)
      }

      ArrayList.prototype.toString = function () {
        return this.array.join()
      }

      ArrayList.prototype.bubbleSort = function () {
        var length = this.array.length
        for (var i = length - 1; i > 0; i--) {
          for (var j = 0; j < i; j++) {
            if (this.array[j] > this.array[j + 1]) {
              this.swap(j, j + 1)
            }
          }
        }
      }

      ArrayList.prototype.selectionSort = function () {
        var length = this.array.length

        for (var i = 0; i < length - 1; i++) {
          var min = i
          for (var j = min + 1; j < length; j++) {
            if (this.array[min] > this.array[j]) {
              min = j
            }
          }
          this.swap(min, i)
        }
      }

      ArrayList.prototype.swap = function (m, n) {
        var temp = this.array[m]
        this.array[m] = this.array[n]
        this.array[n] = temp
      }
    }

    // 初始化数据项
    var list = new ArrayList()

    list.insert(62)
    list.insert(4)
    list.insert(64)
    list.insert(30)
    list.insert(2)
    list.insert(11)
    list.insert(10)
    list.insert(5)
    debugger
    list.selectionSort()
    console.log(list)
  </script>
</body>

</html>